<?php
namespace app\interfaces\controller;

use app\interfaces\model\ShopGoodsEvaluate as ShopGoodsEvaluateModel;
use think\Db;
use think\Request;
use think\Exception;
use think\Controller;

class MerchantEvaluate extends Controller
{

    protected $beforeActionList = [
        'view' => ['only' => 'evaluate_list'],
    ];

    /*
     * 商家端 - 评价管理
     * @param  string $shop_id  店铺ID
     */
    public function evaluate_list()
    {

        // 获取参数
        $shop_id = Request::instance()->param('shop_id');

        if (empty($shop_id)) throw new Exception('not Empty');

        $result = Db::view('shop_goods_evaluate', 'id,member_id,goods_id,picArr,content,grade,title,attr,status,reply_status,create_time')
            ->view('member', 'id as member_id,nickname,avatar', 'shop_goods_evaluate.member_id = member.id')
            ->where(['status' => '2', 'reply_status' => '2', 'shop_id' => $shop_id])->paginate(6)->toArray();

        // 闭包
        $data = array_map(function ($dispose) {
            if (is_array($dispose)) {
                foreach ($dispose as &$item) {

                    if (!empty($item['picArr'])) {
                        $item['picArr'] = unserialize($item['picArr']);
                    }else{
                        $item['picArr'] = [];
                    }

                    $item['create_time'] = date('Y-m-d', $item['create_time']);
                }
            }
            return $dispose;
        }, $result);


        return ['code' => 200, 'message' => '成功', 'data' => $data];
    }

    /*
     * 商家端 - 历史回复管理
     * @param  string $shop_id  店铺ID
     */
    public function evaluate_list_reply()
    {

        // 获取参数
        $shop_id = Request::instance()->param('shop_id');

        if (empty($shop_id)) throw new Exception('not Empty');

        $result = Db::view('shop_goods_evaluate', 'id,member_id,goods_id,picArr,content,grade,title,attr,status,reply,reply_status,create_time')
            ->view('member', 'id as member_id,nickname,avatar', 'shop_goods_evaluate.member_id = member.id')
            ->where(['status' => '1', 'reply_status' => '1', 'shop_id' => $shop_id])
            ->paginate(6)
            ->toArray();

        // 闭包
        $data = array_map(function ($dispose) {
            if (is_array($dispose)) {
                foreach ($dispose as &$item) {

                    if (!empty($item['picArr'])) {
                        $item['picArr'] = unserialize($item['picArr']);
                    }else{
                        $item['picArr'] = [];
                    }

                    $item['create_time'] = date('Y-m-d', $item['create_time']);
                }
            }
            return $dispose;
        }, $result);


        return ['code' => 200, 'message' => '成功', 'data' => $data];
    }

    /*
     * 商家端 - 店铺回复
     * @param  string $id       列表ID
     * @param  string $reply    店铺回复
     * @param  string $user_id  会员ID
     */
    public function evaluate_reply()
    {

        if (!Request::instance()->isPost()) return ['code' => 400, 'message' => '提交方式错误'];

        // 获取参数
        $id = Request::instance()->param('id');
        $reply = Request::instance()->param('reply');
        $user_id = Request::instance()->param('user_id');

        if (empty($id) || empty($reply) || empty($user_id)) throw new Exception('not Empty');

        // 启动事务
        Db::startTrans();
        try {

            Db::name('shop_goods_evaluate')->where(['id' => $id])->update(['reply' => $reply, 'reply_status' => '1']);


            Db::name('member_message')->insert(['member_id' => $user_id, 'title' => '店主已回复您的评价,请查看。', 'content' => $reply, 'create_time' => time()]);

            // 提交事务
            Db::commit();

            return ['code' => 200, 'message' => '成功'];

        } catch (\Exception $e) {

            // 回滚事务
            Db::rollback();

            return ['code' => 401, 'message' => '网络繁忙'];
        }

    }

    /*
     * 商家端 - 更改查看状态
     * @param  string $shop_id  店铺ID
     */
    protected function view()
    {
        $shop_id = Request::instance()->param('shop_id');

        if (!empty($shop_id)) {

            $model = new ShopGoodsEvaluateModel();

            $model->save(['examine' => '1'], ['shop_id' => $shop_id]);

        }
    }

}
